package com.shenmazong.fastdfs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.fastdfs.pojo.TbUploadImage;
import com.shenmazong.fastdfs.service.TbUploadImageService;
import com.shenmazong.fastdfs.mapper.TbUploadImageMapper;
import com.shenmazong.utils.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class TbUploadImageServiceImpl implements TbUploadImageService{

    @Autowired
    TbUploadImageMapper tbUploadImageMapper;

    @Override
    public ResultResponse uploadImageFiles(String name, String urls) {

        TbUploadImage tbUploadImage = new TbUploadImage();
        tbUploadImage.setName(name);
        tbUploadImage.setUrls(urls);

        tbUploadImageMapper.insert(tbUploadImage);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse getImage() {
        TbUploadImage tbUploadImage = tbUploadImageMapper.selectOne(
                new QueryWrapper<TbUploadImage>().lambda()
                        .orderByDesc(TbUploadImage::getId)
                        .last("limit 1")
        );

        if(tbUploadImage == null) {
            return ResultResponse.FAILED(404, "没有图片");
        }

        return ResultResponse.SUCCESS(tbUploadImage);
    }
}




